package com.ymq.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ymq.reggie.common.BaseContext;
import com.ymq.reggie.common.R;
import com.ymq.reggie.entity.ShoppingCart;
import com.ymq.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
     @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 购物车添加
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("shopping:{}",shoppingCart);
        //用户
        Long threadLocalId = BaseContext.getThreadLocalId();
        shoppingCart.setUserId(threadLocalId);

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,threadLocalId);
        //添加菜品
         if(shoppingCart.getDishId()!=null){
             lambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

         }else {
             //添加套餐
             lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
         }

        //查询购物车
        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);
         if (one!=null){
             Integer number = one.getNumber();
             one.setNumber(number+1);
         }else {
             one=shoppingCart;
             one.setNumber(1);
         }
         one.setCreateTime(LocalDateTime.now());
         shoppingCartService.saveOrUpdate(one);

        return R.success(one);
    }

    /**
     * 购物车信息
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list (){
        Long threadLocalId = BaseContext.getThreadLocalId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,threadLocalId);
        List<ShoppingCart> list = shoppingCartService.list();


        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> clean(){
       shoppingCartService.cleanShoppingCatr();

        return R.success("删除成功");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        return R.success("删除成功");
    }

}
